package com.ubing.boot.notesforjava.exception;
import com.ubing.boot.notesforjava.response.Response;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @ClassName : GlobalExceptionHandlerAdvice
 * @Description : 全局异常处理
 * @Author : ubing
 * @Date: 2023-02-15 18:54
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {
    //token不合法异常处理
    @ExceptionHandler(value =MalformedJwtException.class)
    public Response unsupportedJwtExceptionHandler(HttpServletRequest req, HttpServletResponse response){
        return Response.tokenError("token不合法");
    }

    //token逾期异常处理
    @ExceptionHandler(value =ExpiredJwtException.class)
    public Response expiredJwtExceptionHandler(HttpServletRequest req, HttpServletResponse response){
        return Response.tokenError("token逾期");
    }

    // 不是特定异常处理
    @ExceptionHandler(Exception.class)
    public Response exceptionHandler(Exception e, HttpServletRequest request){
        log.info(getClass().getName(), e);
        return Response.failure();
    }
}
